package com.czxy.controller;

import com.czxy.domain.Product;
import com.czxy.domain.ProductVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.portlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;

@Controller
@RequestMapping("/user")
public class HelloController {

    @RequestMapping("/show01.action")
    public String show01(Integer id, Model model) {
        System.out.println("前端发送的数据是: =" + id);

        model.addAttribute("data", id);

        return "/show.jsp";
    }

    @RequestMapping("/show02.action")
    public String show02(String name, Double price, Model model) {
        System.out.println("前端发送的数据是: name=" + name + " price" + price);

        model.addAttribute("data", name + " " + price);

        return "/show.jsp";
    }

    @RequestMapping("/show03.action")
    public String show03(Product product, Model model) {
        System.out.println("前端发送的数据是: product=" + product);

        model.addAttribute("data", product);

        return "/show.jsp";
    }

    @RequestMapping("/show04.action")
    public String show04(ProductVo vo, Model model) {
        System.out.println("前端发送的数据是: vo=" + vo);

        model.addAttribute("data", vo);

        return "/show.jsp";
    }

    @RequestMapping("/show05.action")
    public String show05(ProductVo vo, Model model) {
        System.out.println("前端发送的数据是: vo=" + vo);

        model.addAttribute("data", vo);

        return "/show.jsp";
    }


    @RequestMapping("/show06.action")
    public String show06(@RequestParam(name = "id") String userId, Model model) {
        System.out.println("前端发送的数据是: userId=" + userId);

        model.addAttribute("data", userId);

        return "/show.jsp";
    }


    @RequestMapping("/show07.action")
    public String show07(@RequestParam(name = "id", defaultValue = "100") int userId, Model model) {
        System.out.println("前端发送的数据是: userId=" + userId);

        model.addAttribute("data", userId);

        return "/show.jsp";
    }

    @RequestMapping("/show08.action")
    public String show08(Model model) {
        model.addAttribute("data", new Date());

        String s = new Date().toLocaleString();
        System.out.println(s);

        return "/show.jsp";
    }

    @RequestMapping("/show09.action")
    public String show09(String[] ids, Model model) {
        System.out.println("前端发的数据是 ids=" + Arrays.toString(ids));
        model.addAttribute("data", Arrays.toString(ids));
        return "/show.jsp";
    }


    @RequestMapping(value = {"/show11", "/show22"})
    public String show11(Model model) {
        model.addAttribute("data", "嘿嘿");

        return "/show.jsp";
    }

    @RequestMapping(path = "/show12", method = {RequestMethod.GET, RequestMethod.POST})
    public String show12(String[] ids, Model model) {
        System.out.println("前端发送的数据是: ids=" + Arrays.toString(ids));


        model.addAttribute("data", Arrays.toString(ids));
        return "/show.jsp";


    }

    @RequestMapping("/show13")
    public String show13(Model model) {
        System.out.println("重定向");

        model.addAttribute("data", "重定向");

        return "redirect:/show.jsp";
    }

    @RequestMapping("/show14")
    public String show14(Model model) {
        System.out.println("请求转发");

        model.addAttribute("data", "请求转发");

        return "forward:/show.jsp";
    }


    @RequestMapping("/show16")
    public String show16(HttpSession session) {

        session.setAttribute("data", "张三丰");

        return "redirect:/show.jsp";
    }


    @RequestMapping("/show17")
    public void show17(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        System.out.println("请求转发");

        request.getRequestDispatcher("/show.jsp").forward(request, response);

    }





    public ModelAndView show18(){


        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("data","???");

        modelAndView.setViewName("/show.jsp");


        return modelAndView;


    }

    public String show19(Integer money ,Model model){

        System.out.println("访问了show 21 手动制造一个异常");

        if (money > 100){


        }

        return "/show.jsp";
    }












}
